package com.cmcc.es.app.service.impl;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.cmcc.es.app.model.NoticeInfo;
import com.cmcc.es.app.model.Product;
import com.cmcc.es.app.service.NoticeUserService;

@Service
public class NoticeUserServiceImpl implements NoticeUserService {

	@Autowired
	private RedisTemplate<String, String> redisTemplate;
	private static final String SPLIT_STRING = "--";

	HashMap<String, Set<String>> map = new HashMap<String, Set<String>>();

	@Override
	public HashMap<String, Set<String>> noticeUsers(List<NoticeInfo> infos) {

		for (NoticeInfo noticeInfo : infos) {
			StringBuffer keyBuffer = new StringBuffer();
			StringBuffer keyBuffer2 = new StringBuffer();

			List<Product> products = noticeInfo.getProducts();

			if (products != null) {
				for (Product product : products) {
					keyBuffer.append("*").append(SPLIT_STRING);
					// 添加区域id
					keyBuffer.append(noticeInfo.getSourceId()).append(SPLIT_STRING);
					// 添加大中小分类id
					keyBuffer.append(product.getCatalogBigId()).append(SPLIT_STRING).append(product.getCatalogBigId())
							.append(product.getCatalogMiddleId()).append(SPLIT_STRING).append(product.getCatalogBigId())
							.append(product.getCatalogMiddleId()).append(product.getCatalogSmallId());
					// System.out.println("拼接后的Key:" + keyBuffer);
					// 添加关键字BASE64
					keyBuffer2.append("*").append(SPLIT_STRING);
					// 添加区域id
					keyBuffer2.append(noticeInfo.getSourceId()).append(SPLIT_STRING);
					// 添加大中小分类id
					keyBuffer2.append(product.getCatalogBigId()).append(SPLIT_STRING).append(product.getCatalogBigId())
							.append(product.getCatalogMiddleId()).append(SPLIT_STRING);
					// System.out.println("拼接后的Key2:" + keyBuffer2);
					// 初步筛选redis服务器中的keys
					String keyBuf = keyBuffer.toString();
					String keyBuf2 = keyBuffer2.toString();
					Set<String> resultKeySet = new HashSet<String>();
					Set<String> set1 = redisTemplate.keys(keyBuf);
					Set<String> set2 = redisTemplate.keys(keyBuf2);
					resultKeySet.addAll(set1);
					resultKeySet.addAll(set2);
					// System.out.println("符合条件的条数：" + resultKeySet.size());
					// 根据关键字筛选redis中的keys
					String title = null;
					try {
						title = URLEncoder.encode(noticeInfo.getTitle(), "UTF-8");
						// System.out.println("编码后的关键字" + title);
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					// 实例化一个集合存放最终需要的通知用户
					Set<String> resultValueSets = new HashSet<String>();
					// list数组用于存放所有符合条件的完整key值
					List<String> list = new ArrayList<String>();
					// 遍历初筛key集合，得到最终所需集合
					for (String str : resultKeySet) {
						String result[] = str.split("--");
						// System.out.println("符合后缀匹配条件的关键字：" + result[0]);
						Pattern pattern = Pattern.compile(result[0]);
						java.util.regex.Matcher matcher = pattern.matcher(title);
						// 判断关键字是否成功匹配，为真则将元素加入最终集合
						boolean match = matcher.find();
						if (match) {
							list.add(str);
						}
					}
					if (list.size() > 0) {
						resultValueSets = redisTemplate.opsForSet().union(list.get(0), list);
					}
					map.put(noticeInfo.getNoticeId(), resultValueSets);
				}
			}

		}
		return map;
	}
}
